using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library;
using NCS.Library.Components.Portal;
using NCS.Library.Components.Security;
using NCS.Library.Components.Users;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library.CMSPermisions;
using NCS.Module.CMS.Library.Contents;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Distributions;
using NCS.Module.CMS.Library.Zones;
using NCS.Module.CMS.Library.Events;
using NCS.Module.CMS.Library.Feed;

namespace NCS.Module.CMS.Admin.Feed
{
    public partial class FeedContentList : AuthenticatedControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                SelectDate1.SetDate(DateTime.Now.AddDays(-3));
                SelectDate2.SetDate(DateTime.Now);
                LoadZones();
            }            
        }
        private void LoadZones()
        {
            dropZones.Items.Clear();
            dropZones.DataSource = FeedCatalogController.GetAll();
            dropZones.DataBind();
            dropZones.Items.Insert(0, new ListItem("Root", "0"));            
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);           
            btnSearch_Click(null, null);            
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            int totalrecord ;
            //int zoneId = ConvertUtility.ToInt32(dropZones.SelectedValue);
            if (!chkdeploy.Checked)
            {
                if (!CurrentUser.IsSuperAdmin && !CurrentUser.IsAdmin)
                {
                    dtgContentList.DataSource = FeedContentController.GetFeedContents(dropZones.SelectedValue, dtgContentList.PageSize, dtgContentList.PageIndex, out totalrecord, txtkeysearch.Text, SelectDate1.GetCurrentDate(), SelectDate2.GetCurrentDate().AddDays(1), false);

                }
                else
                {
                    dtgContentList.DataSource = FeedContentController.GetFeedContents(dropZones.SelectedValue, dtgContentList.PageSize, dtgContentList.PageIndex, out totalrecord, txtkeysearch.Text, SelectDate1.GetCurrentDate(), SelectDate2.GetCurrentDate().AddDays(1), false); ;
                }
            }
            else
            {
                if (!CurrentUser.IsSuperAdmin && !CurrentUser.IsAdmin)
                {
                    dtgContentList.DataSource = FeedContentController.GetFeedContents(dropZones.SelectedValue, dtgContentList.PageSize, dtgContentList.PageIndex, out totalrecord, txtkeysearch.Text, SelectDate1.GetCurrentDate(), SelectDate2.GetCurrentDate().AddDays(1), true);

                }
                else
                {
                    dtgContentList.DataSource = FeedContentController.GetFeedContents(dropZones.SelectedValue, dtgContentList.PageSize, dtgContentList.PageIndex, out totalrecord, txtkeysearch.Text, SelectDate1.GetCurrentDate(), SelectDate2.GetCurrentDate().AddDays(1), true); ;
                }
            }
            dtgContentList.VirtualItemCount = totalrecord;
           // dtgContentList.RowCommand +=new GridViewCommandEventHandler(dtgContentList_RowCommand);
            dtgContentList.DataBind();
        }

        protected void dtgContentList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex < 0) return;
            DataRowView curData = (DataRowView)e.Row.DataItem;
            HyperLink lnkHeadline = (HyperLink)e.Row.FindControl("lnkHeadline");
            Button cmdDel = (Button)e.Row.FindControl("cmdDel");

            cmdDel.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
            
            lnkHeadline.Text = curData["Title"].ToString();
            lnkHeadline.NavigateUrl = AppEnv.AdminUrlParams("cms_viewcontent") + "&feedcontentid=" + curData["ID"].ToString();
        }

        public void dtgContentList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string contentId = ConvertUtility.ToString(e.CommandArgument);            
            if (e.CommandName == "edit")
            {
                Response.Redirect(AppEnv.AdminUrlParams("cms_createcontent") + "&feedcontentid=" + contentId );

            }
            if (e.CommandName == "deletecontent")
                FeedContentController.ContentDelete(contentId);            
        }
        
    }
}